function rad(d) {
	return (d * Math.PI) / 180.0;
}

// 根据经纬度计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
function getDistances(lat1, lng1, lat2, lng2) {
	var radLat1 = rad(lat1);
	var radLat2 = rad(lat2);
	
	var a = radLat1 - radLat2;
	var b = rad(lng1) - rad(lng2);
		
	var s =
		2 *
		Math.asin(
			Math.sqrt(
				Math.pow(Math.sin(a / 2), 2) +
				Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
			)
		);
	s = s * 6378.137; // EARTH_RADIUS;
	// 输出为公里
	s = Math.round(s * 10000) / 10000;

	var distance = s;
	var distance_str = "";

	if (parseInt(distance) >= 1) {
		// distance_str = distance.toFixed(1) + "km";
		distance_str = distance.toFixed(2) + "km";
	} else {
		// distance_str = distance * 1000 + "m";
		distance_str = (distance * 1000).toFixed(2) + "m";
	}

	//s=s.toFixed(4);

	// console.info('距离是', s);
	// console.info('距离是', distance_str);
	// return s;

	//小小修改，这里返回对象
	let objData = {
		distance: distance,
		distance_str: distance_str,
	};
	// console.log(objData);
	// if (isNaN(objData.distance_str)) {
	// 	objData.distance_str = "0m"
	// }
	return objData.distance_str;
}

// function calculateDistance(lat1, lon1, lat2, lon2) {
//     const R = 6378.137; // 地球半径，单位为千米
//     const dLat = (lat2 - lat1) * Math.PI / 180;
//     const dLon = (lon2 - lon1) * Math.PI / 180;
//     const a = Math.sin(dLat/2) * Math.sin(dLat/2) +
//               Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
//               Math.sin(dLon/2) * Math.sin(dLon/2);
//     const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
//     const distance = R * c;
    
//     return distance; // 返回距离，单位为千米
// }

export default getDistances;